package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.InterventionService;
import com.itheima.service.SetmealService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/intervention")
public class InterventionController {
    @Reference
    private InterventionService interventionService;
    @Reference
    private SetmealService setmealService;

    //通过登录的用户的手机号此用户的所有套餐,以及套餐对应的套餐日期和到诊状态
    @RequestMapping("/findAll")
    public Result findAll(String telephone) {
        try {
            List<Map> list = interventionService.findByTelephone(telephone);
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, list);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }
    //条件查询
    @RequestMapping("/findAllByCondition")
    public Result findAllByCondition(String after,String before, String queryString,String telephone) {
        try {
          //根据日期和套餐名称模糊查询套餐数据
            queryString = URLDecoder.decode(queryString, "utf-8");
           after = URLDecoder.decode(after, "utf-8");
           before = URLDecoder.decode(before, "utf-8");
            List<Map> list=   interventionService.findAllByCondition(before,after,queryString,telephone);
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, list);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }
      //根据套餐id查询
    @RequestMapping("/findById")
    public Result findById(Integer id,String orderDate,String telephone) {
        try {
       Map<String,Object> map= interventionService.findById(id,orderDate,telephone);
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, map);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }  //根据套餐id查询
    @RequestMapping("/findOrderById")
    public Result findOrderById(Integer id,String orderDate,String telephone) {
        try {
       Map<String,Object> map= interventionService.findOrderById(id,orderDate,telephone);
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, map);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }

}

